﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 自动出题
{
    public partial class QuizForm : Form
    {
        private Random random = new Random();
        private int num1, num2, answer, questionCount = 0, score = 0, totalQuestions = 10;
        private string operation;
        private Timer timer;
        private int timeLeft = 10; // 每题时间10秒

        private Label lblQuestion;
        private TextBox txtAnswer;
        private Button btnSubmit;
        private Label lblFeedback;
        private Label lblScore;
        private Label lblTimeLeft;

        public QuizForm()
        {
            InitializeComponent();
            this.Text = "数学加减法测试";
            this.Size = new System.Drawing.Size(400, 200);

            // 创建控件
            lblQuestion = new Label() { Top = 20, Left = 20, Width = 300 };
            txtAnswer = new TextBox() { Top = 50, Left = 20, Width = 100 };
            btnSubmit = new Button() { Text = "提交", Top = 50, Left = 150 };
            lblFeedback = new Label() { Top = 90, Left = 20, Width = 300 };
            lblScore = new Label() { Text = "分数: 0", Top = 120, Left = 20 };
            lblTimeLeft = new Label() { Text = "剩余时间: 10", Top = 120, Left = 150 };

            this.Controls.Add(lblQuestion);
            this.Controls.Add(txtAnswer);
            this.Controls.Add(btnSubmit);
            this.Controls.Add(lblFeedback);
            this.Controls.Add(lblScore);
            this.Controls.Add(lblTimeLeft);

            // 提交按钮点击事件处理
            btnSubmit.Click += new EventHandler(OnSubmit);

            // 初始化定时器
            timer = new Timer();
            timer.Interval = 1000; // 每秒触发一次
            timer.Tick += new EventHandler(OnTimerTick);

            // 出第一道题
            GenerateQuestion();
            timer.Start();
        }

        private void GenerateQuestion()
        {
            if (questionCount < totalQuestions)
            {
                questionCount++;
                num1 = random.Next(1, 100);
                num2 = random.Next(1, 100);
                operation = random.Next(0, 2) == 0 ? "+" : "-";

                switch (operation)
                {
                    case "+":
                        answer = num1 + num2;
                        break;
                    case "-":
                        answer = num1 - num2;
                        break;
                }

                lblQuestion.Text = $"第{questionCount}题: {num1} {operation} {num2} = ?";
                lblFeedback.Text = "";
                txtAnswer.Text = "";
                txtAnswer.BackColor = System.Drawing.Color.White;
                timeLeft = 10;
                lblTimeLeft.Text = "剩余时间: 10";
            }
            else
            {
                EndQuiz();
            }
        }

        private void OnSubmit(object sender, EventArgs e)
        {
            CheckAnswer();
        }

        private void CheckAnswer()
        {
            int userAnswer;
            if (int.TryParse(txtAnswer.Text, out userAnswer))
            {
                switch (userAnswer == answer)
                {
                    case true:
                        score++;
                        lblFeedback.Text = "正确!";
                        txtAnswer.BackColor = System.Drawing.Color.LightGreen;
                        break;
                    default:
                        lblFeedback.Text = $"错误，正确答案是: {answer}";
                        txtAnswer.BackColor = System.Drawing.Color.LightCoral;
                        break;
                }

                lblScore.Text = $"分数: {score}";
            }
            else
            {
                lblFeedback.Text = "请输入一个有效的数字";
            }

            GenerateQuestion();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft--;
                lblTimeLeft.Text = $"剩余时间: {timeLeft}";
            }
            else
            {
                lblFeedback.Text = $"超时! 正确答案是: {answer}";
                txtAnswer.BackColor = System.Drawing.Color.LightCoral;
                GenerateQuestion();
            }
        }

        private void EndQuiz()
        {
            timer.Stop();
            MessageBox.Show($"测试结束！你的得分是: {score}/{totalQuestions}");
            Application.Exit();
        }



    }
}
